package com.totcy.atom.presenter;

import android.content.Context;

import com.totcy.atom.bean.User;
import com.totcy.atom.model.LoginModel;
import com.totcy.atom.presenter.contract.LoginContract;
import com.totcy.baselibrary.base.mvpbase.BasePresenterImpl;

/**
 * description: 登录界面使用的presenter
 * author: Totcy
 * date: 2017/10/31 14:09
 * update: 2017/10/31
 */

public class LoginPresenter extends BasePresenterImpl<LoginContract.View, LoginContract.Model> implements LoginContract.Presenter{

    public LoginPresenter(Context context) {
        super(context);
    }

    @Override
    public LoginContract.Model attachModel() {
        return new LoginModel(mContext);
    }

    @Override
    public void doLogin(String account, String password) {
        if (isViewAttached()) {
            getView().showProgress("正在登录…");
        }
        getModel().doLogin(account, password, new LoginContract.Model.DataCallback() {
            @Override
            public void onLoginResult(User user) {
                if (isViewAttached()) {
                    getView().dismissProgress();
                    getView().onLoginResult(user);
                }
            }

            @Override
            public void onLoginFail(String message) {
                if (isViewAttached()) {
                    getView().dismissProgress();
                    getView().onLoginFail(message);
                }
            }
        });
    }
}
